package com.abe.config;

import java.util.Arrays;

import android.annotation.SuppressLint;

import com.abe.mvp.model.bean.DeviceBean;
import com.abe.mvp.model.bean.SerialPortDataBean;
import com.abe.mvp.model.bean.SerialPortParseBean;
import com.abe.utils.CRCChecker;

/** 跑步机串口相关 */
@SuppressLint("UseValueOf")
public class TreadmillSericalPortUtils {
	
	//01 03 0C 00 0A 00 02 00 01 00 00 00 00 00 00 BB 08
	/** 串口相关 */
	public static final int SerialPort_ttys0 = 0x001;
	public static final int SerialPort_ttys3 = 0x002;
	public static final int baudrate = 9600;
	public static final String path_ttys0 = "/deviceBean/ttys0";
	public static final String path_ttys3 = "/deviceBean/ttys3";
	/** 协议相关 */
	/** 根据地址区分功能 */
	public static final byte address_all_read = 0x00; // 读全部
	public static final byte address_speed = 0x01; // 速度
	public static final byte address_slope = 0x02;// 坡度
	public static final byte address_start_stop = 0x03;// 启动or停止
	public static final byte address_safe = 0x04;// 安全信号只读0.正常1.急停
	public static final byte address_fan = 0x05;// 风机启停读写0.停止1 启动
	public static final byte address_bpm = 0x06;// 心率数据只读实际值=寄存器值
	/** 读操作or写操作 */
	public static final byte type_read = 0x03;
	public static final byte type_write = 0x06;

	/**************************** 跑步返回数据解析 ******************************/

	public static SerialPortParseBean parse(SerialPortDataBean bean) {
		SerialPortParseBean parseData = new SerialPortParseBean();
		/** 返回原始数据 */
		byte[] data = bean.getBuffer();
		/** 读操作or写操作 */
		byte userTypeData = data[1];
		byte[] available;
		boolean verifyStatus;
		byte address;
		int userType;
		if (userTypeData == type_read) {
			/** 读操作数据解析 */
			userType = 0;
			address = address_all_read;
			if (data.length < 17) {
				verifyStatus = false;
			} else {
				available = Arrays.copyOf(data, 17);
				verifyStatus = CRCChecker.checkBuf(available);
				if (verifyStatus) {
					DeviceBean deviceBean = new DeviceBean();
					/** 速度 */
					float speed = (float) (byteToInt(available[4]))
							/ (float) 10;
					int slope = byteToInt(available[6]);
					boolean deviceStart = byteToInt(available[8]) == 1 ? true
							: false;
					boolean deviceSafe = byteToInt(available[10]) == 0 ? true
							: false;
					boolean fanStart = byteToInt(available[12]) == 1 ? true
							: false;
					int bpm = byteToInt(available[14]);
					deviceBean.setSpeed(speed);
					deviceBean.setSlope(slope);
					deviceBean.setDeviceStart(deviceStart);
					deviceBean.setDeviceSafe(deviceSafe);
					deviceBean.setFanStart(fanStart);
					deviceBean.setBpm(bpm);
					parseData.setReadData(deviceBean);
				}
			}
		} else {
			/** 写操作数据解析 */
			userType = 1;
			if (data.length < 6) {
				verifyStatus = false;
				address = 0x00;
			} else {
				available = Arrays.copyOf(data, 6);
				address = available[3];
				verifyStatus = CRCChecker.checkBuf(available);
			}
		}
		parseData.setVerifyStatus(verifyStatus);
		parseData.setAddress(address);
		parseData.setUseType(userType);
		return parseData;
	}

	public static int byteToInt(byte data) {
		return data & 0xFF;
	}

	/**************************** 跑步机命令集合 ******************************/

	/**
	 * 获取设置速度命令
	 * 
	 * @param speed
	 *            速度值：km/s
	 * */
	public static byte[] getSetSpeedCommand(float speed) {
		int value = (int) (speed * 10);
		/** 取值范围判断 */
		if (10 > value || value > 200) {
			return null;
		}
		byte byte_value = new Integer(value).byteValue();
		byte[] command = new byte[8];
		for (int i = 0; i < command.length; i++) {
			if (i < 5) {
				command[i] = SET_SPEED[i];
			} else {
				command[i] = 0x00;
			}
		}
		command[5] = byte_value;
		return CRCChecker.getSendBuf(command);
	}

	/**
	 * 获取设置坡度命令
	 * 
	 * 
	 * */
	public static byte[] getSetSlopeCommand(float slope) {
		int value = (int) slope;
		/** 取值范围判断 */
		if (0 > value || value > 15) {
			return null;
		}
		byte byte_value = new Integer(value).byteValue();
		byte[] command = new byte[8];
		for (int i = 0; i < command.length; i++) {
			if (i < 5) {
				command[i] = SET_SLOPE[i];
			} else {
				command[i] = 0x00;
			}
		}
		command[5] = byte_value;
		return CRCChecker.getSendBuf(command);
	}

	/**
	 * @see 实际值=寄存器值*10
	 * @see 速度1km/h-20km/h ---- 0x0a-0xc8
	 */
	public static byte[] SET_SPEED = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x01, (byte) 0x00 };

	public static byte[] READ_SPEED = { (byte) 0x01, (byte) 0x03, (byte) 0x00,
			(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00 };

	/**
	 * @see 实际值=寄存器值
	 * @see 坡度0-15 ------0x00-0x96
	 */
	public static byte[] SET_SLOPE = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x02, (byte) 0x00 };

	/**
	 * @see 设置启动 1---0x01
	 * @see 设置关闭 0---0x00
	 * */
	public static byte[] SET_START = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x03, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00 };

	public static byte[] SET_STOP = { (byte) 0x01, (byte) 0x06, (byte) 0x00,
			(byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };

	/**
	 * @see 设置风机启动 1---0x01
	 * @see 设置风机关闭 0---0x00
	 * */
	public static byte[] SET_FAN_START = { (byte) 0x01, (byte) 0x06,
			(byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00,
			(byte) 0x00 };

	public static byte[] SET_FAN_STOPT = { (byte) 0x01, (byte) 0x06,
			(byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00,
			(byte) 0x00 };

	/**
	 * @see 读数据
	 * @see 速度：实际值=寄存器值*10
	 * @see 坡度 ：实际值=寄存器值
	 * @see 设备状态：实际值=寄存器值
	 * @see 安全信号：实际值=寄存器值
	 * @see 风机：实际值=寄存器值
	 * @see 心率：实际值=寄存器值
	 */
	public static byte[] READ_DATA = { (byte) 0x01, (byte) 0x03, (byte) 0x00,
			(byte) 0x01, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x00 };
}